package com.znh.kuiklyuniversity.compose.mainPage

import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import com.tencent.kuikly.compose.ComposeContainer
import com.tencent.kuikly.compose.coil3.rememberAsyncImagePainter
import com.tencent.kuikly.compose.foundation.Image
import com.tencent.kuikly.compose.foundation.layout.Arrangement
import com.tencent.kuikly.compose.foundation.layout.Box
import com.tencent.kuikly.compose.foundation.layout.Column
import com.tencent.kuikly.compose.foundation.layout.ExperimentalLayoutApi
import com.tencent.kuikly.compose.foundation.layout.FlowRow
import com.tencent.kuikly.compose.foundation.layout.PaddingValues
import com.tencent.kuikly.compose.foundation.layout.Row
import com.tencent.kuikly.compose.foundation.layout.aspectRatio
import com.tencent.kuikly.compose.foundation.layout.fillMaxSize
import com.tencent.kuikly.compose.foundation.layout.fillMaxWidth
import com.tencent.kuikly.compose.foundation.layout.height
import com.tencent.kuikly.compose.foundation.layout.padding
import com.tencent.kuikly.compose.foundation.layout.width
import com.tencent.kuikly.compose.foundation.lazy.LazyColumn
import com.tencent.kuikly.compose.foundation.lazy.grid.GridCells
import com.tencent.kuikly.compose.foundation.lazy.grid.LazyVerticalGrid
import com.tencent.kuikly.compose.foundation.lazy.grid.rememberLazyGridState
import com.tencent.kuikly.compose.foundation.pager.VerticalPager
import com.tencent.kuikly.compose.foundation.pager.rememberPagerState
import com.tencent.kuikly.compose.foundation.shape.RoundedCornerShape
import com.tencent.kuikly.compose.material3.Text
import com.tencent.kuikly.compose.resources.DrawableResource
import com.tencent.kuikly.compose.resources.InternalResourceApi
import com.tencent.kuikly.compose.resources.painterResource
import com.tencent.kuikly.compose.ui.Alignment
import com.tencent.kuikly.compose.ui.Modifier
import com.tencent.kuikly.compose.ui.draw.clip
import com.tencent.kuikly.compose.ui.graphics.Color
import com.tencent.kuikly.compose.ui.input.pointer.pointerInput
import com.tencent.kuikly.compose.ui.layout.ContentScale
import com.tencent.kuikly.compose.ui.text.style.TextAlign
import com.tencent.kuikly.compose.ui.unit.dp
import com.tencent.kuikly.compose.ui.unit.sp
import com.tencent.kuikly.core.base.BorderStyle
import com.tencent.kuikly.core.base.attr.ImageUri
import com.tencent.kuikly.core.module.Module
import com.tencent.kuikly.core.nvi.serialization.json.JSONObject
import com.znh.kuiklyuniversity.Border
import com.znh.kuiklyuniversity.backgroundColor
import com.znh.kuiklyuniversity.border
import com.znh.kuiklyuniversity.compose.selfenum.AppColor
import com.znh.kuiklyuniversity.compose.widgets.UserView
import com.znh.kuiklyuniversity.height
import com.znh.kuiklyuniversity.padding
import com.znh.kuiklyuniversity.width
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

import com.tencent.kuikly.core.module.CallbackFn
@OptIn(ExperimentalLayoutApi::class, InternalResourceApi::class)
@Composable
fun MyPage(ct: ComposeContainer) {

    var bridge=BridgeModule()
    bridge.toast("我的")
    val pageState = rememberPagerState(
        initialPage = 0,
        pageCount = { ListModel.dataBgList.size }
    )
    val scope = rememberCoroutineScope()
    var imgIndex = 0
    scope.launch {
        while (true) {
            delay(3000)
            imgIndex++
            if (imgIndex >= ListModel.dataBgList.size) {
                imgIndex = 0
            }
            pageState.scrollToPage(imgIndex)
        }
    }

    var editLayout: Array<String> = arrayOf(
        "icon/ic_action_barcode_2.png",
        "icon/ic_action_bargraph.png",
        "icon/ic_action_camera.png",
        "icon/ic_action_brush.png",
        "icon/ic_action_crop.png",
        "icon/ic_action_globe.png",
        "icon/ic_action_gmail.png"
    )

    var textEditText: Array<String> = arrayOf(
        "唐诗三百首",
        "我的系统",
        "关于我们",
        "隐私服务",
        "设置",
        "商场",
        "配置中心"
    )

    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        VerticalPager(
            state = pageState,
            pageContent = { index ->
                Image(
                    painter = painterResource(
                        DrawableResource(
                            ImageUri.commonAssets(ListModel.dataBgList.get(index))
                                .toUrl("")
                        )
                    ),
                    contentScale = ContentScale.Crop,
                    contentDescription = "",
                    modifier = Modifier.fillMaxSize(),
//                    alpha = 0.5f
                )
            }
        )



        LazyColumn(
            modifier = Modifier.fillMaxSize()
                .padding(start = 16.dp, end = 16.dp, top = 24.dp)
        ) {

            item {
                UserView(ct)
            }

            item {
                Box(modifier = Modifier.height(10.dp))
            }
            item {
                Column(
                    modifier = Modifier.fillMaxWidth()
                        .backgroundColor(Color.White).clip(
                            RoundedCornerShape(10.dp)
                        )
                ) {
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        modifier = Modifier.height(45.dp)
                            .fillMaxWidth()
                            .backgroundColor(AppColor.BgSelColor.color)
                            .clip(RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp)),
                    )
                    {
                        Text(
                            "可编辑栏目",
                            color = AppColor.TxtSelColor.color,
                            fontSize = 15.sp,
                            maxLines = 1,
                            modifier = Modifier.padding(start = 10.dp)
                        )
                    }

                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(220.dp)
                    ) {

                        LazyVerticalGrid(
                            userScrollEnabled = false,// 属性加以下内容，禁止滚动
                            modifier = Modifier.pointerInput(Unit) {
                                awaitPointerEventScope {
                                    while (true) {
                                        awaitPointerEvent()
                                    }
                                }
                            },
                            columns = GridCells.Fixed(4),
                            contentPadding = PaddingValues(16.dp),
                            horizontalArrangement = Arrangement.spacedBy(10.dp),
                            verticalArrangement = Arrangement.spacedBy(10.dp),
                            content = {
                                items(editLayout.size) {
                                    Column(
                                        modifier = Modifier
                                            .fillMaxWidth()
                                            .aspectRatio(ratio = 1f),
                                        horizontalAlignment = Alignment.CenterHorizontally
                                    ) {
                                        Image(
                                            painter = rememberAsyncImagePainter(
                                                ImageUri.SCHEME_COMMON_ASSETS + editLayout.get(
                                                    it
                                                )
                                            ),
                                            contentDescription = "编辑栏目",
                                            modifier = Modifier
                                                .fillMaxWidth()
                                                .aspectRatio(ratio = 1f)
                                                .padding(5.dp)
                                                .clip(RoundedCornerShape(5.dp))
                                                .border(
                                                    Border(
                                                        lineWidth = 1.dp,
                                                        color = AppColor.BgSelColor.color,
                                                        lineStyle = BorderStyle.SOLID
                                                    )
                                                )


                                        )
                                    }

                                }
                            }
                        )
                    }

                }
            }

            item {
                Box(modifier = Modifier.height(10.dp))
            }
            item {
                Column(
                    modifier = Modifier.fillMaxWidth()
                        .backgroundColor(Color.White).clip(
                            RoundedCornerShape(10.dp)
                        )
                ) {
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        modifier = Modifier.height(45.dp)
                            .fillMaxWidth()
                            .backgroundColor(AppColor.BgSelColor.color)
                            .clip(RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp)),
                    )
                    {
                        Text(
                            "文本类型",
                            color = AppColor.TxtSelColor.color,
                            fontSize = 15.sp,
                            maxLines = 1,
                            modifier = Modifier.padding(start = 10.dp)
                        )
                    }

                    FlowRow(
                        modifier = Modifier
                            .padding(10.dp)
                    ) {
                        repeat(textEditText.size) {
                            Box(
                                modifier = Modifier
                                    .clip(RoundedCornerShape(2.dp))
                                    .padding(5.dp)
                                    .border(
                                        Border(
                                            lineWidth = 1.dp,
                                            color = AppColor.BgSelColor.color,
                                            lineStyle = BorderStyle.SOLID
                                        )
                                    )
                            ) {
                                Text(
                                    text = textEditText.get(it),
                                    modifier = Modifier
                                        .padding(
                                            start = 10.dp,
                                            end = 10.dp,
                                            top = 5.dp,
                                            bottom = 5.dp
                                        )

                                )
                            }
                        }
                    }
                }
            }
        }
    }
}


internal class BridgeModule : Module() {

    override fun moduleName(): String {
        return "MyPage"
    }
    fun toast(content: String) {
//        KLog.i("znh","----------------Toast-----------------")
        val methodArgs = JSONObject()
        methodArgs.put("content", content)
        callNativeMethod("toast", methodArgs,{ data->
            var aa=1
        })
    }
    internal fun callNativeMethod(methodName: String, data: JSONObject?, callbackFn: CallbackFn?) {
        toNative(
            false,
            methodName,
            data?.toString(),
            callbackFn,
            false
        )
    }


}